from django.shortcuts import render,HttpResponse
from myadmin.models import *  #导入后台中的所有模型
from django.contrib.auth.hashers import make_password, check_password
from django.http import JsonResponse
import json
from myadmin.views.user import uploads_pic
def member_order(request):
    #查看是否付款的状态  如果没有参数 则表示全部
    if request.GET.get('status'):
        if request.GET.get('status')=='1':
            #print('11')
            order=Order.objects.filter(uid=request.session.get('VipUser')['uid'],status=1)
        #未付款状态注释掉 现在只有两个状态  如果是其他状态 则报错
        #elif request.GET.get('status')=='0':
        else:
            order=Order.objects.filter(uid=request.session.get('VipUser')['uid'],status=0)

        context = {'order': order}
        return render(request, 'myhome/member_order.html', context)
    #查询所有该用户下的 订单
    order=Order.objects.filter(uid=request.session.get('VipUser')['uid'])
    #推送数据
    context={'order':order}

    return  render(request,'myhome/member_order.html',context)

def member(request):
    ob=Users.objects.get(id=request.session.get('VipUser')['uid'])
    order=Order.objects.filter(uid=ob.id)
    #print(order)
    order_status={'payok':0,'payno':0}
    for i in order:
        if i.status==0:
            order_status['payok']+=1
            #print('已经付款')

        elif i.status==1:
            order_status['payno']+=1
            #print('没有付款')
    #把订单状态传递到首页
    data={"user":ob,"order":order_status}
    return render(request,'myhome/member.html',data)

def member_save(request):

    email=request.POST.get('email',None)
    password=request.POST.get('password',None)
    #上传文件需要使用files.get 才能获取
    myfile = request.FILES.get('pic', None)
    ob=Users.objects.get(id=request.session.get('VipUser')['uid'])
    #print(ob.id)
    if email:
        ob.email=email
    if password:
        ob.password=make_password(password)
    if myfile:
        ob.pic_url=uploads_pic(myfile)
    #print(myfile,'111')
    ob.save()
    return HttpResponse('<script>alert("更新成功");location.href="/member/"</script>')
